package com.ys168.gam.shortestpath.persistent;

import static com.ys168.gam.shortestpath.uimessage.UIMessageUtil.getString;

import java.io.File;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.ys168.gam.shortestpath.model.Node;
import com.ys168.gam.shortestpath.uimessage.UIMessageCode;
import com.ys168.gam.shortestpath.util.FileUtil;

/**
 *
 * @author Jason Krebs
 * @date 2015年2月7日
 */
public class DataExporter {

    private static final String JSON_FORMAT = "{\"node\":[%s],\"line\":[%s]}";
    private static final String LINE_FORMAT = "{\"start\":%d,\"end\":%d}";
    private static final String NODE_FORMAT = "{\"index\":%d,\"location\":{\"x\":%.2f,\"y\":%.2f}}";
    private static final String COMMA = ",";

    public static void exportTo(List<Node> nodes, String path) {
        String data = String.format(JSON_FORMAT, toExportNodes(nodes), toExportLine(nodes));
        FileUtil.saveTo(path, data);
    }

    private static String toExportLine(List<Node> nodes) {
        StringBuilder sb = new StringBuilder();
        for (Node start : nodes) {
            for (Node end : start.getOutNodes()) {
                sb.append(toLineString(start, end)).append(COMMA);
            }
        }
        if (sb.length() > 0) {
            sb.delete(sb.length() - COMMA.length(), sb.length());
        }
        return sb.toString();
    }

    private static String toExportNodes(List<Node> nodes) {
        StringBuilder sb = new StringBuilder();
        for (Node node : nodes) {
            sb.append(toNodeString(node)).append(COMMA);
        }
        if (sb.length() > 0) {
            sb.delete(sb.length() - COMMA.length(), sb.length());
        }
        return sb.toString();
    }

    private static String toLineString(Node start, Node end) {
        return String.format(LINE_FORMAT, start.getIndex(), end.getIndex());
    }

    private static String toNodeString(Node node) {
        return String.format(NODE_FORMAT, node.getIndex(), node.getLocation().getX(), node.getLocation().getY());
    }

    public static void doExport(List<Node> nodes) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(getString(UIMessageCode.MESSAGE_03));
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setSelectedFile(new File("nodes"));
        chooser.setFileFilter(new FileNameExtensionFilter("*.json", "json"));
        if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            File selected = chooser.getSelectedFile();
            if (selected != null) {
                String path = selected.getPath();
                if (!selected.getPath().contains(".")) {
                    FileNameExtensionFilter currentFilter = (FileNameExtensionFilter) chooser.getFileFilter();
                    path += "." + currentFilter.getExtensions()[0];
                }
                DataExporter.exportTo(nodes, path);
                JOptionPane.showMessageDialog(null, String.format(getString(UIMessageCode.MESSAGE_04), path));
            }
        }
    }

}
